package org.exhibition.mapper;

import org.apache.ibatis.annotations.*;
import org.exhibition.domain.entity.ExhibitionTicketEntity;
import org.exhibition.domain.pojo.exhibition.ExhibitionList;
import org.exhibition.domain.pojo.exhibition.ExhibitionOrder;
import org.exhibition.domain.pojo.exhibition.ExhibitionTicket;
import org.exhibition.domain.pojo.exhibition.UserTicketOrder;


import java.time.LocalDate;
import java.util.List;

@Mapper
public interface ExhibitionMapper {
    @Select("SELECT *FROM exhibition")
    List<ExhibitionList> getAllExhibition();

    @Select("select *From exhibition where id=#{id}")
    //根据id获取展览
    ExhibitionList getExhibitionById(Integer id);

    //获取指定时间段内的展览
    @Select("select name from exhibition_ticket_view where exhi_date=#{startDate}")
    List<String> getExhibitionByDate(LocalDate startDate);

    //根据名称获取展览
    @Results({
            @Result(property = "date", column = "exhi_date"),
            @Result(property = "availableTickets", column = "exhi_number"),
            @Result(property = "ticketPrice", column = "exhi_price")
    })
    @Select("select exhi_date,exhi_number,exhi_price from exhibition_ticket_view where name=#{name} and exhi_date=#{startDate} ")
    ExhibitionTicket getExhibitionByName(String name, LocalDate startDate);

    @Select("select exhibition_ticket.id, exhibition.name,exhibition.start_time,exhibition.end_time,exhibition.location,exhibition_ticket.exhi_price as ticketPrice from exhibition inner join exhibition_ticket on exhibition.id=exhibition_ticket.exhibition_id where exhibition.name=#{name} and exhi_date=#{startDate}")
    //根据名称获取展览详情
    ExhibitionList getExhibitionDetailByName(String name, LocalDate startDate);

    //提交展览预约
    @Insert("insert into user_ticket(identity,exhi_ticket_id,ticket_type,ticket_cost,create_time,user_id) values(#{identity},#{ticketId},#{ticketType},#{ticketCost},#{createTime},#{phone})")
    void submitExhibitionReservation(UserTicketOrder userTicketOrder);

    //展览已销售票数增加
    @Update("update exhibition_ticket set paid_number=paid_number+1 where id=#{id} and paid_number<exhi_number")
    int updateExhibitionTicket(Integer id);

    //判断当前的已售出票数是否大于总票数
    @Select("select count(*)  from user_ticket where exhi_ticket_id=#{id} and `identity`=#{identity}")
    int getExhibitionTicketById(Integer id,String identity);

    //根据名称获取展览id
    @Select("select id from exhibition where name=#{name}")
    String getExhibitionIdByName(String name);

    //获取对应id的所有订单
    @Select("select * from user_ticket where user_id=#{phone}")
    List<UserTicketOrder> getAllExhibitionReservation(String phone);

    //根据订单id获取获取票的id
    @Select("select exhi_ticket_id from user_ticket where id=#{id}")
    Long getExhibitionTicketIdById(Integer id);

    //根据票的id获取展览的id
    @Select("select exhibition_id,exhi_price as price from exhibition_ticket where id= #{id}")
    ExhibitionTicketEntity getExhibitionIdById(Integer id);
}
